<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>轮播图点击切换</title>
  <style>
    * {
      box-sizing: border-box;
    }

    .slider {
      width: 560px;
      height: 660px;
      overflow: hidden;
    }

    .slider-wrapper {
      width: 100%;
      height: 560px;
    }

    .slider-wrapper img {
      width: 100%;
      height: 100%;
      display: block;
    }

    .slider-footer {
      height: 80px;
      background-color: rgb(100, 67, 68);
      padding: 12px 12px 0 12px;
      position: relative;
    }

    .slider-footer .toggle {
      position: absolute;
      display: flex;
      right: 0;
      top: 12px;
    }

    .slider-footer .toggle button {
      margin-right: 12px;
      width: 28px;
      height: 28px;
      border: none;
      appearance: none;
      color: #fff;
      background-color: rgba(255, 255, 255, 0.1);
      border-radius: 4px;
      cursor: pointer;
    }

    .slider-footer .toggle button:hover {
      background-color: rgba(255, 255, 255, 0.2);
    }

    .slider-footer p {
      margin: 0;
      color: #fff;
      font-size: 18px;
      margin-bottom: 10px;
    }

    .slider-indicator {
      margin: 0;
      padding: 0;
      display: flex;
      list-style: none;
      align-items: center;
    }

    .slider-indicator li {
      width: 8px;
      height: 8px;
      margin: 4px;
      border-radius: 50%;
      background: #fff;
      opacity: 0.4;
      cursor: pointer;
    }

    .slider-indicator li.active {
      width: 12px;
      height: 12px;
      opacity: 1;
    }
  </style>
</head>

<body>
  <div class="slider">
    <div class="slider-wrapper">
      <img src="../pic/1.jpg" alt="">
    </div>
    <div class="slider-footer">
      <p>王菲</p>
      <ul class="slider-indicator">
        <li class="active"></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
      </ul>
      <div class="toggle">
        <button class="prev">&lt;</button>
        <button class="next">&gt;</button>
      </div>
    </div>
  </div>
  <script>
    // 1.初始数据
    const data = [    //注意下标是0-7
      { url: '../pic/1.jpg', title: '王菲', color: 'rgb(100, 67, 68)' },    //下标为0
      { url: '../pic/2.jpg', title: '陈奕迅', color: 'rgb(43, 35, 26)' },   //下标为1
      { url: '../pic/3.jpg', title: '李克勤', color: 'rgb(36, 31, 26)' },   //2
      { url: '../pic/4.jpg', title: '张学友', color: 'rgb(139, 98, 66)' },  //3
      { url: '../pic/5.jpg', title: '林俊杰', color: 'rgb(67, 90, 92)' },   //4
      { url: '../pic/6.jpg', title: '周杰伦', color: 'rgb(166, 131, 143)' },//5
      { url: '../pic/7.jpg', title: '刘德华', color: 'rgb(53, 29, 25)' },   //6
      { url: '../pic/8.jpg', title: '周柏豪', color: 'rgb(99, 72, 114)' },  //7
    ]
    // 获取元素
    const img = document.querySelector('.slider-wrapper img')
    const p = document.querySelector('.slider-footer p')
    const footer = document.querySelector('.slider-footer')
    let i = 0

    // 1.右侧按钮
    // 1.1获取右侧按钮
    const next = document.querySelector('.next')
    // 1.2注册点击事件
    next.addEventListener('click', function () {
      i++
      // if (i >= data.length) { i = 0 }
      i = i >= data.length ? 0 : i
      // 1.3得到对应对象
      // console.log(data[i])
      // 调用函数
      toggle()
    })

    // 2.左侧按钮
    // 2.1获取左侧按钮
    const prev = document.querySelector('.prev')
    // 2.2注册点击事件
    prev.addEventListener('click', function () {
      i--
      i = i < data.length - data.length ? data.length - 1 : i
      // 2.3得到对应对象
      // console.log(data[i])
      // 调用函数
      toggle()
    })

    // 声明一个渲染的函数作为复用
    function toggle() {
      // 1.4渲染对应的数据
      img.src = data[i].url
      p.innerHTML = data[i].title
      footer.style.backgroundColor = data[i].color
      // 1.5更换小圆点   先移除原来的类名，当前li再添加这个类名
      document.querySelector('.slider-indicator .active').classList.remove('active')
      document.querySelector(`.slider-indicator li:nth-child(${i + 1})`).classList.add('active')
    }

    // 3.自动播放模块
    let timeId = setInterval(function () {
      next.click()
    }, 800)

    const slider = document.querySelector('.slider')
    // 4.鼠标路过大盒子，停止定时器 （先关再开）
    // 注册事件
    slider.addEventListener('mouseenter', function () {
      // 停止定时器
      clearInterval(timeId)
    })

    // 5.鼠标离开大盒子，开启定时器
    // 注册事件
    slider.addEventListener('mouseleave', function () {
      timeId = setInterval(function () {
        next.click()
      }, 800)
    })
  </script>
</body>

</html>